/*
 * ClientAcceptor.h
 *
 *  Created on: 08.12.2011
 *      Author: Pavel Sinay
 */

#ifndef CLIENTACCEPTOR_H_
#define CLIENTACCEPTOR_H_

#include <string>
#include "Socket.h"
#include <sys/select.h>

namespace net {

class ClientAcceptor {
public:
	ClientAcceptor();
	virtual ~ClientAcceptor();

	void start(unsigned short port);
	void stop();
	unsigned short getPort() const;

protected:
	virtual void onClientAccepted(Socket *client_socket) = 0;
	void throwException(const std::string &msg);

private:
	pthread_t m_thread;
	pthread_attr_t m_attr;
	bool m_stop_req;
	bool m_started;
	int m_fd_server_socket;
	fd_set m_readset;
	unsigned short m_port;

	int startServer(unsigned short port);
	void stopServer();
	static void *thread_operation(void *this_);
	void listening();
};

}

#endif /* CLIENTACCEPTOR_H_ */
